package com.gxa.finance.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.common.pojo.ServiceDetails;
import com.gxa.common.pojo.ServiceNode;
import com.gxa.common.pojo.ServiceTag;
import com.gxa.finance.pojo.Pricing;
import com.gxa.finance.pojo.ServiceManagement;
import com.gxa.finance.service.ServiceManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@RestController
@RequestMapping("/finance/management")
@Slf4j
public class ServiceManagementController {

    @Autowired
    private ServiceManagementService serviceManagementService;
    @Autowired
    ServiceTagController serviceTagController;
    @Autowired
    PricingController pricingController;
    @Autowired
    ServiceNodeController serviceNodeController;
    @Autowired
    ServiceDetailsController serviceDetailsController;

    //推荐服务查询
   @GetMapping("/Page")
    public JsonResult page(@RequestParam Map<String, Object> map) {
       System.out.println("============"+map.get("country"));
       System.out.println(map.get("type"));
        JsonResult jsonResult = new JsonResult();
        try{
            //参数处理
            int currentPage = Integer.parseInt(map.get("currentPage").toString());
            int pageSize = Integer.parseInt(map.get("pageSize").toString());

            //查询的条件
            QueryWrapper<ServiceManagement> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(map.get("country")!=null && !map.get("country").toString().isEmpty(),"country",map.get("country"))
                    .eq(map.get("type")!=null && !map.get("type").toString().isEmpty(),"type",map.get("type"))
                    .ge(map.get("starTime")!=null && !map.get("starTime").toString().isEmpty(),"time",map.get("starTime"))
                    .le(map.get("endTime")!=null && !map.get("endTime").toString().isEmpty(),"time",map.get("endTime"));//时间 判断不为空

            //分页信息
            Page<ServiceManagement> page = new Page<>(currentPage,pageSize);
            Page<ServiceManagement> resultList = serviceManagementService.page(page, queryWrapper);

            //响应信息
            jsonResult.setCode(200);
            jsonResult.setMsg("success");
            jsonResult.setCount((int) resultList.getTotal());
            jsonResult.setData(resultList.getRecords());
        }
        catch(Exception e){
            e.printStackTrace();
            jsonResult.setCode(401);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }

        return  jsonResult;
    }

    //服务管理分页查询
    @GetMapping("/ServicePage")
    public R ServicePage(Integer currentPage , Integer pageSize){
        //返回查出来的数据
        R r = serviceManagementService.ServicePage(currentPage, pageSize);
        return r;
    }

    //服务类型条件查询
    @GetMapping("/ifService")
    public R ifService(ServiceManagement serviceManagement){
        Page<ServiceManagement> page = new Page(1,10,false);
        QueryWrapper<ServiceManagement> queryWrapper = new QueryWrapper<>();
        //根据服务类型搜索服务推荐信息
        if(StringUtils.isNotBlank(serviceManagement.getType())&&StringUtils.isNotEmpty(serviceManagement.getType())){
            queryWrapper.eq("type",serviceManagement.getType());
            System.out.println("service====="+serviceManagement.getType());
        }
        if (StringUtils.isNotBlank(serviceManagement.getCountry())&&StringUtils.isNotEmpty(serviceManagement.getCountry())){
            queryWrapper.eq("country",serviceManagement.getCountry());
            System.out.println("service====="+serviceManagement.getCountry());
        }
//        if(StringUtils.isNotBlank(serviceManagement.getTime().toString())&&StringUtils.isNotEmpty(serviceManagement.getTime().toString())){
//            queryWrapper.eq("time",serviceManagement.getTime());
//            System.out.println("service====="+serviceManagement.getTime());
//        }

        R r = serviceManagementService.ifService(page,queryWrapper);
        return r;
    }

    //服务类型状态（启用）
    @PostMapping("/static")
    public R state(ServiceManagement serviceManagement){
        //返回查出来的数据
        R r = serviceManagementService.state(serviceManagement);
        return r;
    }

    ////服务类型状态（禁用）
    @PostMapping("/noStatic")
    public R noState(ServiceManagement serviceManagement){
        //返回查出来的数据
        R r = serviceManagementService.noState(serviceManagement);
        return r;
    }


    //分页查询 推荐服务 （1/2推荐/不推荐）
    @GetMapping("/manager")
    public R manager(Integer currentPage,Integer pageSize){
        R r = serviceManagementService.manager(currentPage, pageSize);
        return r;
    }

    //推荐服务 （已推荐/未推荐 状态接口）
    @PostMapping("/recommend")
    public R recommend(ServiceManagement serviceManagement){
        //返回查出来的数据
        R r = serviceManagementService.recommend(serviceManagement);
        return r;
    }


    //未推荐 状态
    @PostMapping("/noRecommend")
    public R noRecommend(ServiceManagement serviceManagement){
        R r = serviceManagementService.noRecommend(serviceManagement);
        return r;
    }

    //启用/禁用
    @PostMapping("/open")
    public R open(String Id ,String status){
       return serviceManagementService.open(Id,status);
    }
    //推荐/不推荐
    @PostMapping("/tui")
    public R tui(String Id ,String statue){
        return serviceManagementService.tui(Id,statue);
    }



    //查询有/无定价
    @GetMapping("/queryFixedPrice")
    public R queryFixedPrice(ServiceManagement serviceManagement){

        R r = new R();
        Map<String,Object> map = new HashMap<>();

        //查询基础信息
        ServiceManagement primaryMsg = serviceManagementService.getById(serviceManagement.getId());
        //查询标签
        List<ServiceTag> tagList = (List<ServiceTag>) serviceTagController.findServiceTagList(serviceManagement.getServe(), serviceManagement.getPrice()).getData();
        //查询定价信息
        List<Pricing> pricingList = (List<Pricing>) pricingController.findPricing(serviceManagement.getType()).getData();
        //查询节点信息
        List<ServiceNode> serviceNodeList = (List<ServiceNode>) serviceNodeController.findServiceNodeList(serviceManagement.getServe(), serviceManagement.getPrice()).getData();
        //查询服务详情
        List<ServiceDetails> serviceDetailsList = (List<ServiceDetails>) serviceDetailsController.findServiceDetailsList(serviceManagement.getServe(), serviceManagement.getPrice()).getData();

        //数据封装
        map.put("primaryMsg",primaryMsg);
        map.put("tagList",tagList);
        if(serviceManagement.getPrice()==1){
            map.put("pricingList",pricingList);
        }
        map.put("serviceNodeList",serviceNodeList);
        map.put("serviceDetailsList",serviceDetailsList);

        //返回数据
        r.setCode(200);
        r.setMsg("success");
        r.setData(map);
        return r;
    }
}

